<?php
namespace my;
/**
 * 分页类
 * @author xls
 */
class Page{

    /**
     * 返回当前页页码
     * @param unknown $total     总条数
     * @param number  $page      页码
     * @param number  $page_size 每页显示条数
     * @return 页码
     */
    public static function curPage($total, $page = 1,  $page_size = 12){
        $page = (int)($page);
        if( $page <= 1 )
            return 1;
        
        $max_page = ceil($total/$page_size);
        $max_page = $max_page <= 1 ? 1 : $max_page;
          
        return $page >= $max_page ? $max_page : $page;
    }
    
    /**
     * 返回分页数据
     * @param unknown $total 总条数
     * @param unknown $page  当前页码
     * @param unknown $page_size  每页显示条数
     * @return 分页原始数据
     */
    public static function getData( $total, $page, $page_size ){
        $total_page_num = ceil($total / $page_size);
        $temp = [
            'list'              => [],  #数字目录
            'first'             => '',  #首页
            'last'              => '',  #尾页
            'pre'               => '',  #上一页
            'next'              => '',  #下一页
            'cur'               => '',  #当前页
            'total'             => '',  #总条数
            'total_page_num'    => '',  #总页数
        ];
        
        //数字目录
        if( $page < 7 ){
            $start = 0;
            $end   = $total_page_num <= 6 ? $total_page_num : 7;
        }else if($page >= 7 && $page < $total_page_num-5){
            $start = $page - 4;
            $end   = $page + 4;
        }else if( $page > $total_page_num-7 ){
            $start = $total_page_num - 7;
            $end   = $total_page_num;
        }
        for($i = $start; $i < $end; $i++){
            $temp['list'][] = $i+1;
        }
        
        //快捷页码
        if ( !empty($temp['list']) ) {
            //首页 尾页
            $temp['first'] = !empty($temp['list']) && min($temp['list']) > 1 ? 1 : "";
            $temp['last']  = !empty($temp['list']) && max($temp['list']) < $total_page_num ? $total_page_num : "";
        
            //上一页 下一页
            $temp['pre']   = $page == 1 ? "" : $page - 1;
            $temp['next']  = $page == $total_page_num ? "" : $page + 1;
            $temp['cur']   = $page;
        
            //总条数 总页数
            $temp['total']          = $total;
            $temp['total_page_num'] = $total_page_num;
        }
        
        return $temp;
    }
}